package com.zhangc.blog.admin.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.zhangc.blog.admin.entity.BlogInfoEntity;
import com.zhangc.blog.admin.service.ArticleService;
import com.zhangc.blog.admin.service.BlogInfoServiceImpl;
import com.zhangc.blog.base.utils.FileUtils;

/**
 * 功能描述:<br>
 *
 * @author wx:fdzhangc
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/api/blog")
public class BlogController {

    @Autowired
    ArticleService articleService;
    @Autowired
    FileUtils fileUtils;
    @Autowired
    BlogInfoServiceImpl blogInfoService;

    //新增或编辑文章
    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable(name = "id") Long id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("edit");
        if (id == null) {
            modelAndView.addObject("article", new BlogInfoEntity());
            return modelAndView;
        }
        BlogInfoEntity article = blogInfoService.getById(id);
        if (article == null) {
            modelAndView.addObject("article", new BlogInfoEntity());
        } else {
            modelAndView.addObject("article", article);
        }
        return modelAndView;
    }

    //发布文章
    @RequestMapping("/publish")
    @ResponseBody
    public String publishArticle(BlogInfoEntity article) {
        if (article == null) {
            return "false";
        }
        boolean res = blogInfoService.saveOrUpdate(article);
        if (res) {
            return "success";
        }
        return "false";
    }

    //图片上传
    @RequestMapping("/image/upload")
    @ResponseBody
    // 注意RequestParam中的name，不可改。
    public JSONObject imageUpload(@RequestParam("editormd-image-file") MultipartFile image) {
        JSONObject jsonObject = new JSONObject();
        if (image != null) {
            String path = fileUtils.uploadFile(image);
            System.out.println(path);
            jsonObject.put("url", path);
            jsonObject.put("success", 1);
            jsonObject.put("message", "upload success!");
            return jsonObject;
        }
        jsonObject.put("success", 0);
        jsonObject.put("message", "upload error!");
        return jsonObject;
    }

    //加载单个文章,不可编辑
    @RequestMapping("/get/{id}")
    public ModelAndView getArticleById(@PathVariable(name = "id") Long id) {
        ModelAndView modelAndView = new ModelAndView();
        BlogInfoEntity article = blogInfoService.getById(id);
        modelAndView.setViewName("article");
        if (article == null) {
            modelAndView.addObject("article", new BlogInfoEntity());
        } else {
            modelAndView.addObject("article", article);
        }
        return modelAndView;
    }
}
